/*
 *  GuiButton.cpp
 *  SpaceTrader
 *
 *  Created by Jake on 9/21/12.
 *  Copyright 2012 __MyCompanyName__. All rights reserved.
 *
 */

#include "GuiButton.h"


/*
 * Handles button events
 */

GuiButton::GuiButton(int x, int y) : state(0), label(NULL), bounds(SDL_Rect()), icon(load_image("GuiButton.png", false))
{
	if (icon == NULL) {
		std::cerr << "Error: Loading default GuiButton Image" << std::endl;
	}
	
	bounds.x = x;
	bounds.y = y;
	bounds.w = icon->w/2;
	bounds.h = icon->h;
	clip[0].x = 0;
	clip[0].y = 0;
	clip[0].w = 148;
	clip[0].h = icon->h;
	
	clip[1].x = 148;
	clip[1].y = 0;
	clip[1].w = 148;
	clip[1].h = icon->h;
	
	clip[2].x = 296;
	clip[2].y = 0;
	clip[2].w = 148;
	clip[2].h = icon->h;
}


GuiButton::GuiButton(int x, int y, std::string type, std::string text) :  state(0), label(NULL), bounds(SDL_Rect()), icon(NULL)
{
    
    if (text != ""){
        SDL_Color labelColor = {255,255,255};
        label = TTF_RenderText_Blended(getFont(text), text.c_str(), labelColor); //we possibly need to close this font
    }

    
    if (type == ""){
        icon = load_image("GuiButton.png",false);
        if (icon == NULL) {
            std::cerr << "Error: Loading default GuiButton Image" << std::endl;
        }
        
        bounds.x = x;
        bounds.y = y;
        bounds.w = icon->w/2;
        bounds.h = icon->h;
        
        clip[0].x = 0;
        clip[0].y = 0;
        clip[0].w = 148;
        clip[0].h = icon->h;
        
        clip[1].x = 148;
        clip[1].y = 0;
        clip[1].w = 148;
        clip[1].h = icon->h;
        
        clip[2].x = 296;
        clip[2].y = 0;
        clip[2].w = 148;
        clip[2].h = icon->h;
        
    } else {
        icon = load_image(type + ".png",false);
        if (icon == NULL) {
            std::cerr << "Error: Loading " << type << " GuiButton Image" << std::endl;
        }
        if("GuiArrowL" == type) {
            bounds.x = x;
            bounds.y = y;
            bounds.w = 50;
            bounds.h = 50;
            state = 0;
            //sprite clips
            clip[0].x = 0;
            clip[0].y = 0;
            clip[0].w = 50;
            clip[0].h = 50;
            
            clip[1].x = 51;
            clip[1].y = 0;
            clip[1].w = 50;
            clip[1].h = 50;
            
            clip[2].x = 102;
            clip[2].y = 0;
            clip[2].w = 50;
            clip[2].h = 50;
        } else if("GuiArrowR" == type) {
            bounds.x = x;
            bounds.y = y;
            bounds.w = 50;
            bounds.h = 50;
            state = 0;
            //sprite clips
            clip[0].x = 0;
            clip[0].y = 0;
            clip[0].w = 50;
            clip[0].h = 50;
            
            clip[1].x = 50;
            clip[1].y = 0;
            clip[1].w = 50;
            clip[1].h = 50;
            
            clip[2].x = 100;
            clip[2].y = 0;
            clip[2].w = 50;
            clip[2].h = 50;
        } else if ("GuiArrowU" == type) {
			bounds.x = x;
			bounds.y = y;
			bounds.w = 50;
			bounds.h = 50;
			state = 0;
			//sprite clips
			clip[0].x = 0;
			clip[0].y = 0;
			clip[0].w = 50;
			clip[0].h = 50;
			
			clip[1].x = 0;
			clip[1].y = 50;
			clip[1].w = 50;
			clip[1].h = 50;
			
			clip[2].x = 0;
			clip[2].y = 100;
			clip[2].w = 50;
			clip[2].h = 50;
		} else if ("GuiArrowD" == type) {
			bounds.x = x;
			bounds.y = y;
			bounds.w = 50;
			bounds.h = 50;
			state = 0;
			//sprite clips
			clip[0].x = 0;
			clip[0].y = 100;
			clip[0].w = 50;
			clip[0].h = 50;
			
			clip[1].x = 0;
			clip[1].y = 50;
			clip[1].w = 50;
			clip[1].h = 50;
			
			clip[2].x = 0;
			clip[2].y = 0;
			clip[2].w = 50;
			clip[2].h = 50;
		} else if ("SmallGuiButton" == type){
            bounds.x = x;
			bounds.y = y;
			bounds.w = 111;
			bounds.h = 41;
			state = 0;
			//sprite clips
			clip[0].x = 0;
			clip[0].y = 0;
			clip[0].w = 111;
			clip[0].h = 41;
			
			clip[1].x = 111;
			clip[1].y = 0;
			clip[1].w = 111;
			clip[1].h = 41;
			
			clip[2].x = 222;
			clip[2].y = 0;
			clip[2].w = 111;
			clip[2].h = 41;
            
            free(label);
            SDL_Color labelColor = {255,255,255};
            label = TTF_RenderText_Blended(TTF_OpenFont("Futura.ttc", 20), text.c_str(), labelColor); //we possibly need to close this font
        }
    }
}

GuiButton::GuiButton(const GuiButton &G) : state(G.state), label(G.label), bounds(G.bounds), icon(G.icon)
{
    for (unsigned int i = 0; i < 4; i++){
        clip[i] = G.clip[i];
    }
}

GuiButton::~GuiButton()
{
	SDL_FreeSurface(icon);
    if (label != NULL){
        SDL_FreeSurface(label);
    }
}

GuiButton& GuiButton::operator=(const GuiButton &G){
    if (&G != this){
        delete icon;
        delete label;
        
        state = G.state;
        label = G.label;
        bounds = G.bounds;
        icon = G.icon;
        for (unsigned int i = 0; i < 4; i++){
            clip[i] = G.clip[i];
        }
    }
    return *this;
}

button_state_t GuiButton::handle_input(SDL_Event event)
{
	//If a key was pressed
	if (event.type == SDL_MOUSEBUTTONDOWN) {
		SDL_Rect mouse;
		mouse.x = event.button.x;
		mouse.y = event.button.y;
		mouse.w = 2;
		mouse.h = 2;
		if (check_collision(bounds, mouse)) {
			//std::cout << "Clicked" << std::endl;
			return CLICKED;
		}
	} else if (event.type == SDL_MOUSEMOTION) {
		SDL_Rect mouse;
		mouse.x = event.motion.x;
		mouse.y = event.motion.y;
		mouse.w = 2;
		mouse.h = 2;
		if (check_collision(bounds, mouse)) {
			//std::cout << "Hover" << std::endl;
			state = 1;
			return MOUSEOVER;
		} else {
			state = 0;
			return NONE;
		}
	}
	return NONE;
}
void GuiButton::setState(int nextState)
{
	state = nextState;
}

int GuiButton::getState()
{
	return state;
}


/*
 *	Renders the button on the screen 
 */
void GuiButton::show(SDL_Surface *screen) 
{
	apply_surface_clip(bounds.x, bounds.y, clip[state], icon, screen);
    if (label != NULL){
        apply_surface(bounds.x+clip[state].w/2-label->w/2, bounds.y+clip[state].h/2-label->h/2, label, screen);
    }
}


